var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pid:'',
    collect:'',
    showid: '',
    id:'',
    loading:true,
    lock:false,
    c_lock:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      article_id:options.article_id
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight
        })
      }
    })
    that.getData();
    that.seecollect();
  },
  seecollect:function(){
    var that = this;
    //查看文章是否已经收藏
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'article',
          act: 'is_collect',
          article_id: that.data.article_id,
          t_session: session
        },
        success: function (res) {
          var data = res.data;
          if (data.done && data.retval) {
            that.setData({
              collect: 1,
              id: app.turn(data.retval)[0].id
            })

          }
        }
      })
    })
  },
  getData:function(){
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'article',
          act: 'getArticleDetail',
          article_id: that.data.article_id,
          t_session:session
        },
        success: function (res) {
          var data = res.data;
          if (data.done && data.retval) {
            that.setData({
              detail: data.retval.article[0],
              comments: data.retval.comments,
              zan: data.retval.article[0].is_zan,
              time: util.formatDate(new Date(data.retval.article[0].add_time * 1000), 'YYYY-MM-dd'),
              loading:false
            })
            WxParse.wxParse('article', 'html', data.retval.article[0].content.replace(/embed/g, "video"), that);
          }
        },
        fail:function(res){
          that.setData({
            loading:false,
            loadTips:'app:getArticleDetail连接失败'
          })
        }
      })
    })
    
  },
// 评论文章
  comment:function(res){
    var that = this;
    var comment = res.detail.value;
    if(comment!=''&&comment!=undefined){
      //发表评论
      app.getLoginSession(function(session){
        wx.request({
          url: app.api,
          data: {
            app: 'article',
            act: 'getAddComment',
            article_id: that.data.article_id,
            pid: that.data.pid,
            content: comment,
            t_session:session
          },
          success:function(res){
            var data = res.data;
            if(data.done&&data.retval.code==1){
              wx.showToast({
                title: data.retval.msg,
                icon:'success'
              })
              that.getData();
            }else{
              wx.showToast({
                title: '评论失败',
                icon:'loading'
              })
            }
          }
        })
      })
      
    }
  },
  //文章点赞
  zanarticle:function(res){
    var that = this;
    if(res.currentTarget.dataset.zan==0){
      // 加锁
      if (that.data.lock) return false;
      that.setData({
        lock: true
      });
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api,
          data: {
            app: 'article',
            act: 'getZanArticle',
            article_id: that.data.article_id,
            t_session:session
          },
          success: function (res) {
            var data = res.data;
            if (data.done && data.retval.code == 1) {
              that.setData({
                zan: 1
              })
              that.getData();
            } else {
              wx.showToast({
                title: '点赞失败',
                icon: 'loading'
              })
            }
          },
          // 请求结束
          complete: function () {
            // 解锁
            that.setData({
              lock: false
            })
          }
        })
      })
    }
    
  },
  // 显示全部评论
  showall:function(e){
  var that = this;
  console.log(e);
  var showid = e.currentTarget.dataset.id;
  if (that.data.showid == showid){
    that.setData({
      showid:"a"
    })
  }else{
    that.setData({
      showid: showid
    })
  }
  console.log(that.data.showid);
  },
  // 回复
  showbox:function(e){
    var that = this;
    var name = e.currentTarget.dataset.name;
    that.setData({
      box:true,
      name:name,
      pid:e.currentTarget.dataset.pid
    })
  },
  cancle:function(){
    var that = this;
    that.setData({
      box:that.data.box == true ? false : true
    })
  },
  //失去焦点
  onbindblur:function(event){
    var that = this;
    console.log(event);
    that.setData({
      comment: event.detail.value
    })
    console.log(that.data.comment);
  },
  //点击发送
  submit_send:function(){
    var that = this;
    console.log(that.data.comment);
    if (that.data.comment != '' && that.data.comment != undefined) {
      //发表评论
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api,
          data: {
            app: 'article',
            act: 'getAddComment',
            article_id: that.data.article_id,
            pid: that.data.pid,
            content: that.data.comment,
            t_session: session
          },
          success: function (res) {
            var data = res.data;
            if (data.done && data.retval.code == 1) {
              that.setData({
                box: that.data.box == true ? false : true,
                pid: '',
                comment: ''
              })
              wx.showToast({
                title: '评论成功',
                icon: 'success'
              })
              that.getData();
            } else {
              that.setData({
                pid: ''
              })
              wx.showToast({
                title: '评论失败',
                icon: 'loading'
              })
            }
          },
          fail: function (res) {
            that.setData({
              pid: ''
            })
          }
        })
      })

    }
  },
  //点击完成
  submit_box:function(event){
    console.log("aaa");
    var that = this;
    that.setData({
      comment: event.detail.value||that.data.comment
    })
    console.log(event)
    if (that.data.comment != '' && that.data.comment != undefined) {
      //发表评论
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api,
          data: {
            app: 'article',
            act: 'getAddComment',
            article_id: that.data.article_id,
            pid: that.data.pid,
            content: that.data.comment,
            t_session: session
          },
          success: function (res) {
            var data = res.data;
            if (data.done && data.retval.code == 1) {
              that.setData({
                box: that.data.box == true ? false : true,
                pid:'',
                comment:''
              })
              wx.showToast({
                title: '评论成功',
                icon: 'success'
              })
              that.getData();
            } else {
              that.setData({
                pid:''
              })
              wx.showToast({
                title: '评论失败',
                icon: 'loading'
              })
            }
          },
          fail:function(res){
            that.setData({
              pid:''
            })
          }
        })
      })

    }
  },
  //评论点赞
  laud:function(e){
    var that = this;
    // 加锁
    if (that.data.c_lock == true) return false;
    that.setData({
      c_lock: true
    });
    if(e.currentTarget.dataset.zan==0){
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api,
          data: {
            app: 'article',
            act: 'getZanComment',
            id: e.currentTarget.dataset.id,
            t_session: session
          },
          success: function (res) {
            var data = res.data;
            if (data.done && data.retval.code == 1) {
              that.getData();
            } else {
              wx.showToast({
                title: '点赞失败',
                icon: 'loading'
              })
            }
          },
          // 请求结束
          complete: function () {
            // 解锁
            that.setData({
              c_lock: false
            })
          }
        })
      })
    }
    
    
  },
  onShareAppMessage: function () {
    var that = this;
    return {
      path: '/pages/articledetail/articledetail',
      success: function (res) {
        that.setData({
          imgs: true
        })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  //文章收藏
  collect:function(res){
      var that = this;
      if(that.data.collect==1){
        that.setData({
          act:'dropCollectArticle'
        })
      }else{
        that.setData({
          act: 'collectArticle'
        })
      }
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api,
          data:{
            app:'article',
            act:that.data.act,
            article_id:that.data.article_id,
            id:that.data.id,
            t_session:session
          },
          success:function(res){
            var data = res.data;
            if(data.done&&data.retval.code==1){
              that.setData({
                collect:that.data.collect==1?0:1
              })
              that.seecollect();
            }else{
              wx.showToast({
                title: '失败',
                icon:'loading'
              })
            }
          }
        })
      })
      
  },
  // 关闭悬浮图片
  closeimg: function () {
    var that = this;
    that.setData({
      imgs:false
    })
  },
  // deallist(list) {
  //   if (list && Array.isArray(list)) {
  //     list.forEach((item) => {
  //       switch (parseInt(item.content_type)) {
  //         case 1:
  //           item.pageType = 'article'
  //           break
  //         case 2:
  //           item.pageType = 'gallery'
  //           break
  //         case 3:
  //           item.pageType = 'video'
  //           break
  //       }
  //     })
  //     return list
  //   }
  // }
})